package com.yuwen.controller;

import com.yuwen.pojo.Users;
import com.yuwen.service.impl.UserService;
import com.yuwen.utils.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
//@CrossOrigin("*")
@CrossOrigin(origins = "http://localhost:8080",allowCredentials = "true")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/user/login")
    public int login(@RequestBody Users users){
        int id = users.getUid();//前端
        String pwd = users.getPassword();//前端
        try {
            Users user_ = userService.findUser(id);//后端
            //System.out.printf("前端："+users);
            //System.out.printf("后端："+user_);
            String pwd_ = user_.getPassword();
            if (pwd_.equals(pwd)) {//存在
                return 200;//登录成功
            }else{//失败
                return 500;
            }
        }catch (Exception e){
            return 500;
        }
    }
    @PostMapping("/user/registry")//http://localhost:8083/user/add
    public int add(@RequestBody Users users){//接收前端返回的封装好的数据对象
        userService.save(users);
        return 200;
    }




    @PostMapping("/user/update")//
    public Result update(@RequestBody Users users){//接收前端返回的封装好的数据对象
        userService.save(users);
        return Result.success();
    }
    @GetMapping("/user/getlist")//http://localhost:8083/user/getlist
    public Result<List<Users>> findAll() {
        return Result.success(userService.findAll());
    }

    // 删除用户
    @DeleteMapping("/user/delete/{uid}")//http://localhost:8083/user/delete/100002
    public void delete(@PathVariable("uid") Integer uid) {
        userService.del(uid);
    }

    // 根据id查询用户
    @GetMapping("/user/query/{uid}")//http://localhost:8083/user/query/100002
    public Result<Users> findById(@PathVariable Integer uid) {
        //System.out.println(userService.findById(uid).getUid());
        return Result.success(userService.findById(uid));
    }
}
